package com.lib.controller.regi;

import java.sql.Date;
import java.sql.Timestamp;
import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import oracle.sql.DATE;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.lib.model.dto.MemberDto;
import com.lib.model.dto.ZipCodeDto;


@Controller
public class RegiController {
	
	@Autowired
	private com.lib.model.regi.IMemberServie service;
	@Autowired
	private com.lib.model.service.IIDCheckService idc;


		@RequestMapping("/r_popup.do")
		public ModelAndView regipopup(HttpServletRequest request, HttpServletResponse response){
			
			ModelAndView mav = new ModelAndView();
			
			String input_id = request.getParameter("input_id");
			request.setAttribute("input_id", input_id);
			mav.setViewName("regipopup");
			
			return mav;
		}//
		
		@RequestMapping("/zipopen.do")
		public String zipopen(){
			return "address";
		}
		
		@RequestMapping("/zipcode.do")
		public ModelAndView zipCode(HttpServletRequest request, HttpServletResponse response){
			
			ModelAndView mav = new ModelAndView();
			
			String dong = request.getParameter("dong").trim();
			ArrayList<ZipCodeDto> zlist = service.search(dong);
			
			request.setAttribute("dong", dong);
			request.setAttribute("zip", zlist);
			mav.setViewName("address");
			
			
			return mav;
		}
		
		@RequestMapping("/idcheck.do")
		public ModelAndView idCheck(HttpServletRequest request, HttpServletResponse response){
			MemberDto dto = null;
			boolean flag = false;
			ModelAndView mav = new ModelAndView();
			String id = request.getParameter("input_id");
			dto = idc.idCheck(id.trim());

			
			
			if(dto==null){
				flag = false;
			}else{
				flag = true;
			}
			
			request.setAttribute("input_id", id);
			request.setAttribute("flag",flag);
			mav.setViewName("regipopup");
			
			return mav;
		}
		
		@RequestMapping("/addmember.do")
		public ModelAndView addMember(
															 @RequestParam("id") String id
															,@RequestParam("password")String password
															,@RequestParam("username")String name
															,@RequestParam("minnum1")String minnum1
															,@RequestParam("minnum2")String minnum2
															,@RequestParam("birthday")String birthday
															,@RequestParam("h1")String h1
															,@RequestParam("h2")String h2
															,@RequestParam("h3")String h3
															,@RequestParam("p1")String p1
															,@RequestParam("p2")String p2
															,@RequestParam("p3")String p3
															,@RequestParam("e1")String e1
															,@RequestParam("e2")String e2
															,@RequestParam("addnum1")String addnum1
															,@RequestParam("addnum2")String addnum2
															,@RequestParam("addr1")String addr1
															,@RequestParam("addr2")String addr2
															){
			
			ModelAndView mav = new ModelAndView();
			 
			MemberDto dto = new MemberDto(	0
															,id
															,password
															,name
															,minnum1 + "-" + minnum2
															, Date.valueOf(birthday)
															, h1+"-"+h2+"-"+h2
															, p1+"-"+p2+"-"+p3
															, e1+"@"+e2
															, addnum1+"-"+addnum2
															, addr1
															, addr2
															, 0
															, null
															, null
															);
			
			service.addMember(dto);
			mav.setViewName("complete");
			return mav;
		}
		


	
	
}
